/**
 * 新增用户
 * @url admin/system/user/sys/add
 * @description 新增用户
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '新增成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let {
			username,
			nickname,
			password,
			mobile,
			email,
			role = [],
			status = 0
		} = data;
		
		// 参数验证
		if (!username) {
			return { code: -1, msg: '用户名不能为空' };
		}
		if (!nickname) {
			return { code: -1, msg: '昵称不能为空' };
		}
		if (!password) {
			return { code: -1, msg: '密码不能为空' };
		}
		
		// 检查用户名是否已存在
		let existUser = await db.collection('uni-id-users')
			.where({ username })
			.get();
		if (existUser.data.length > 0) {
			return { code: -1, msg: '用户名已存在' };
		}
		
		// 使用 uni-id 创建用户
		let createRes = await uniID.createUser({
			username,
			password,
			nickname,
			mobile,
			email,
			role,
			status
		});
		
		if (createRes.code !== 0) {
			return { code: -1, msg: createRes.msg || '创建用户失败' };
		}
		
		res.data = createRes.uid;
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

